package com.epro.dx.task;

import com.epro.dx.util.ActivityUtil;
import com.weipu.common.facade.exception.DxException;
import com.weipu.common.facade.factory.ProviderFactory;
import com.weipu.common.facade.support.service.task.BaseServiceTask;


/************************
 * @author Tony
 * @Date:2016-4-29下午3:20:30
 * @描述:收到消息后的回执接口任务
 ************************/
public class FeedBackNoticeTask extends
		BaseServiceTask<Integer> {

	private static final long serialVersionUID = -6221560093333502736L;
	private String noticeID;
	private int noticeType;
	public FeedBackNoticeTask(String noticeID,final int noticeType){
		this.noticeID = noticeID;
		this.noticeType = noticeType;
		setNeedBroadcast2UI(true);
		setWhatForUICallback(BaseServiceTask.FROM_NOTICE_FEEDBACK);
	}
	@Override
	protected boolean executeRequest() {
		return true;
	}
	@Override
	protected Integer execute() {
		int reTryCount = 0;
        
        String result = null;//接口返回结果
        do
        {
            reTryCount++;
            
            result = feedBack();
            
            //重试次数超过3次，认为回执失败，
            if (reTryCount > 3)
            {
                break;
            }
            
        } while (!"0".equals(result));//只要不成功则会重试回执，最多尝试3此
		return noticeType;
	}
	/**
	 * @创建：Tony 2016-5-9下午5:04:49
	 * @描述：调用消息回执接口
	 * @return
	 */
	private String feedBack() {
		try {
			ProviderFactory.createMessageService().feedBackNotice(ActivityUtil.getCustomerID(),
					ActivityUtil.getCustomerToken(), noticeID);
		} catch (DxException e) {
			return e.getErrCode();
		}
		return "0";
	}
	@Override
	public int hashCode() {
		return noticeID.hashCode();
	}
}
